package org.newlucene.core.analysis;

public class Util
{
	private static int STRIP_PLURAL_MIN_WORD_SIZE = 2;
	
	/** A useful, but not particularly efficient plural stripper */
	public static String stripEnglishPlural(String word)
	{
		// too small?
		if (word.length() < STRIP_PLURAL_MIN_WORD_SIZE)
		{
			return word;
		}

		// special cases
		if (word.equals("has") || word.equals("was") || word.equals("does")
			|| word.equals("goes") || word.equals("dies")
			|| word.equals("yes") || word.equals("gets") || word.equals("its"))
		{
			return word;
		}
		
		String newWord = word;
		if (word.endsWith("sses") || word.endsWith("xes") || word.endsWith("hes"))
		{
			// remove 'es'
			newWord = word.substring(0, word.length() - 2);
		} 
		else if (word.endsWith("ies"))
		{
			// remove 'ies', replace with 'y'
			newWord = word.substring(0, word.length() - 3) + 'y';
		} 
		else if (word.endsWith("s") && !word.endsWith("ss")
			&& !word.endsWith("is") && !word.endsWith("us")
			&& !word.endsWith("pos"))
			//	&& !word.endsWith("ses") is removed, there are 3 oddities moses=mose gases=gase, viruses=viruse 
			// but I got 100+ extra stems that were ok
		{
			// remove 's'
			newWord = word.substring(0, word.length() - 1);
		}
		return newWord;
	}
}
